<?php
/**
 * CreateTime: 2023/6/26 14:27
 */

namespace App\Tools\QxwgSMS;

use App\Func\AppFunc;
use App\Func\CurlFunc;
use App\Func\TokenFuc;
use App\Models\SystemLog\SmsSystemLog;
use App\Tools\USails\USailsClient;

class QxwgSmsClient
{
    private $appSecret;
    private $appKey;
    private $xnjrm;
    private $url;
    private $requestType = "GET";
    private static $qxwgSmsClient;

    private function __construct()
    {
        $this->appSecret = AppFunc::getQXWGConfig()::QXWG_SECRET;
        $this->appKey = AppFunc::getQXWGConfig()::QXWG_KEY;
        $this->xnjrm = AppFunc::getQXWGConfig()::QXWG_XNJRM;
        $this->url = 'http://'.AppFunc::getQXWGConfig()::QXWG_IP.":".AppFunc::getQXWGConfig()::QXWG_PORT.DIRECTORY_SEPARATOR.'smsv2';
    }

    private function __clone(): void{}

    public static function getInstance(){
        if(!self::$qxwgSmsClient){
            self::$qxwgSmsClient = new QxwgSmsClient();
        }
        return self::$qxwgSmsClient;
    }

    public function changeRequestType($type){
        $this->requestType = $type;
    }



    public function smsEncryption($data){
        $key = 'cJyvQwSUJnPooGF7qKeOqM1JhjLypQOmVbjwwwvDt8k1eJjOt2LncHz4hjFAjVXoLuK3XyffSK8FIeY6HhUmuV2eDClN3FsqqvtiC8jSTFKytgt1hAJI6fBDb1BCnxEpOWsSuwbplc4wpoJnAtehKO8CFAVWxXh1GSyfqTzXUfbG1Jur0b749V56qix5PHHgG73sdrmerqS2V46A5xTckMT0tAINmV0XedNBWlerdA7oIda4j4vm1Not';
//        dump($key);
//        $key = 'RBFyxh37apY29wN47nmrNYPWjxYR4778';
        $cipher = "aes-256-ecb";
        $encrypted = openssl_encrypt($data, $cipher, $key, OPENSSL_RAW_DATA);
        return base64_encode($encrypted);
    }

    public function execute($action,$data){
        $data['action'] = $action;
        $data['account'] = AppFunc::getQXWGConfig()::QXWG_KEY;
        $data['extno'] = AppFunc::getQXWGConfig()::QXWG_XNJRM;
        $publicHeader = [
            'Content-type:application/json',
            'Charset:utf-8'
        ];
        $url = $this->url;
        // 日志哦
        $sslm = new SmsSystemLog();
        switch ($this->requestType){
            case "GET":
                $res =  CurlFunc::CurlGet($url,1,$data,$publicHeader);
                $sslm->add([
                    'request_info' => json_encode($data,256),
                    'response_info' => $res,
                    'type_str' => '企信网关',
                    'request_header' => json_encode($publicHeader,256),
                    'owner_id' => TokenFuc::getCurrentStaffInfo()['staff_id'] ?? 0
                ]);
                return $res;
            case "POST":
            default:
                $res =  CurlFunc::CurlPost($url,$data,$publicHeader);
                $sslm->add([
                    'request_info' => json_encode($data,256),
                    'response_info' => $res,
                    'type_str' => '企信网关',
                    'request_header' => json_encode($publicHeader,256),
                    'owner_id' => TokenFuc::getCurrentStaffInfo()['staff_id'] ?? 0
                ]);
                return $res;
        }
    }
}